package com.dwm716.shorturl.controller;

import com.dwm716.shorturl.common.ApiResponse;
import com.dwm716.shorturl.domain.User;
import com.dwm716.shorturl.dto.CreateUserVO;
import com.dwm716.shorturl.pagehelper.PageQuery;
import com.dwm716.shorturl.utils.RoleEnum;
import com.dwm716.shorturl.utils.UserStatus;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author biliyu
 * @date 2023/6/6 09:39
 */
@RestController
public class UserController extends BaseController{

    @PostMapping("/user/create")
    public ApiResponse<Object> create(@RequestHeader("userId") Integer userId,
                              @RequestBody CreateUserVO createUserVO) {
        createUserVO.setUserId(userId);
        boolean adminUser = isSupperAdminUser(createUserVO.getUserId());
        if (!adminUser) {
            return ApiResponse.fail("用户没有权限");
        }
        List<User> userList = userService.getByName(createUserVO.getName());
        if (!CollectionUtils.isEmpty(userList)) {
            return ApiResponse.fail("用户已经存在");
        }

        User user = new User();
        user.setName(createUserVO.getName());
        user.setPassword(createUserVO.getPassword());
        user.setRole(RoleEnum.Normal.name());
        user.setStatus(UserStatus.Normal.name());
        user.setUserName(createUserVO.getUserName());
        userService.insert(user);
        return ApiResponse.ok();
    }

    @PostMapping("/user/login")
    public ApiResponse<User> userLogin(@RequestBody User user) {
        User loginUser = userService.login(user);
        return ApiResponse.ok(loginUser);
    }

    @GetMapping("/user/info")
    public ApiResponse<User> userInfo(@RequestHeader("userId") Integer userId) {
        User user = userService.getById(userId);
        return ApiResponse.ok(user);
    }


    @PostMapping("/user/update")
    public ApiResponse<Object> update(@RequestHeader("userId") Integer userId, @RequestBody CreateUserVO createUserVO) {
        createUserVO.setUserId(userId);
        boolean adminUser = isSupperAdminUser(createUserVO.getUserId());
        if (!adminUser) {
            return ApiResponse.fail("用户没有权限");
        }
        User user = new User();
        BeanUtils.copyProperties(createUserVO, user);

        userService.update(user);
        return ApiResponse.ok();
    }

    @PostMapping("/user/delete")
    public ApiResponse<Object> delete(@RequestHeader("userId") Integer userId, @RequestBody CreateUserVO createUserVO) {
        createUserVO.setUserId(userId);
        boolean adminUser = isSupperAdminUser(createUserVO.getUserId());
        if (!adminUser) {
            return ApiResponse.fail("用户没有权限");
        }
        User user = new User();
        user.setId(createUserVO.getId());
        user.setStatus(UserStatus.Deleted.name());
        userService.update(user);
        return ApiResponse.ok();
    }

    @PostMapping("/user/pageList")
    public ApiResponse<Object> requestRecordList(@RequestHeader("userId") Integer userId, @RequestBody PageQuery query) {
        query.setUserId(userId);
        boolean adminUser = isSupperAdminUser(query.getUserId());
        if (!adminUser) {
            return ApiResponse.fail("用户没有权限");
        }

        return ApiResponse.ok(userService.list(query.getStatusList(), query.getPageNo(), query.getPageSize()));
    }


}
